<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Hyperf\Guzzle\ClientFactory;
use GuzzleHttp\Client;

/**
 * @Command
 */
class ESInit extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var \Hyperf\Guzzle\ClientFactory
     */
    private $clientFactory;

    public function __construct(ContainerInterface $container, ClientFactory $clientFactory)
    {
        $this->container = $container;
        $this->clientFactory = $clientFactory;
        parent::__construct('es:init');
    }

    public function configure()
    {
        $this->setDescription('ES init');
    }

    public function handle()
    {
//        $this->createTemplate();
        $this->createIndex();
        $this->line('es init success', 'info');
    }

    const HOST = 'http://127.0.0.1:9200';

    public function createTemplate()
    {
        $client = $this->clientFactory->create();
        $response = $client->put(sprintf('%s/_template/template_blog', self::HOST), [
            'json' => [
                'index_patterns' => ['article*', 'blog*'],//Array of wildcard expressions used to match the names of indices during creation.
                'settings' => [
                    'number_of_shards' => 1,
                ],
                'mappings' => [
                    'dynamic_templates' => [ // 动态映射模板
                        [
                            'string_fields' => [ // 字段映射模板的名称，一般为"类型_fields"的命名方式
                                'match' => '*', // 匹配的字段名为所有
                                'match_mapping_type' => 'string', // 限制匹配的字段类型，只能是 string 类型
                                'mapping' => [ // 字段的处理方式
                                    'type' => 'text', // 字段类型限定为 string
                                    'analyzer' => 'ik_smart', // 字段采用的分析器名，默认值为 standard 分析器
                                    'fields' => [
                                        'raw' => [
                                            'type' => 'keyword',
                                            'ignore_above' => 256, // 字段是索引时忽略长度超过定义值的字段。
                                        ]
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    public function createIndex()
    {
        $client = new Client;
        $response = $client->put(sprintf('%s/articles', self::HOST), [
            'json' => [
                'mappings' => [
                    "dynamic" => "strict", //控制字段的新增 文档不能写入报错
                    'properties' => [
                        "title" => [
                            "type" => "text"
                        ],
                        "content" => [
                            'type' => 'text'
                        ],
                        "created_at" => [
                            "type" => "date",
                            "format" => "yyyy-MM-dd HH:mm:ss"
                        ]
                    ]
                ],
            ],
        ]);
        //"{"acknowledged":true,"shards_acknowledged":true,"index":"articles"}"
        var_dump($response->getBody()->getContents());
    }
}
